package service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.RoleGroup_dao;
import dto.RoleGroupDto;
import entity.Group_Menu;
import entity.RoleGroup;
import service.RoleGroup_service;

@Service
public class RoleGroup_service_impl implements RoleGroup_service {

	@Autowired
	private RoleGroup_dao roleGroup_dao;
	
	@Override
	public boolean add_RoleGroup(RoleGroup rolegroup) {
		// TODO Auto-generated method stub
		return roleGroup_dao.add_RoleGroup(rolegroup);
	}

	@Override
	public boolean delete_RoleGroup(Integer id) {
		// TODO Auto-generated method stub
		return roleGroup_dao.delete_RoleGroup(id);
	}

	@Override
	public boolean update_RoleGroup(RoleGroup rolegroup) {
		// TODO Auto-generated method stub
		return roleGroup_dao.update_RoleGroup(rolegroup);
	}

	@Override
	public List<RoleGroupDto> Group_list() {
		List<RoleGroupDto> result=new ArrayList<>();
		List<RoleGroup> list=roleGroup_dao.Group_list();
		for (RoleGroup roleGroup : list) {
			RoleGroupDto dto=new RoleGroupDto();
			BeanUtils.copyProperties(roleGroup, dto);
			dto.setpId(0);
			result.add(dto);
		}
		return result;
	}

	@Override
	public List<Group_Menu> Group_MenuList(Integer groupId) {

		return roleGroup_dao.Group_MenuList(groupId);
	}

	@Override
	public boolean delete_GroupMenu(Integer groupId) {
		return roleGroup_dao.delete_GroupMenu(groupId);
	}

	@Override
	public boolean add_GroupMenu(List<Group_Menu> groupMenuList){

		return roleGroup_dao.add_GroupMenu(groupMenuList);
	}

	@Override
	public boolean assignMenu(Integer groupId, String menuIds) {
		if(groupId!=null&&menuIds!=null){
			roleGroup_dao.delete_GroupMenu(groupId);
			String[] menuids=menuIds.split(",");
			List<Group_Menu> groupMenuList=new ArrayList<Group_Menu>();
			for (int i = 0; i < menuids.length; i++) {
				Group_Menu group_Menu=new Group_Menu();
				group_Menu.setGroupId(groupId);
				group_Menu.setMenuId(Integer.parseInt(menuids[i]));
				groupMenuList.add(group_Menu);
			}
			return roleGroup_dao.add_GroupMenu(groupMenuList);
		}
		return false;
	}

	@Override
	public RoleGroup getRoleGroupByName(String roleName) {
		return roleGroup_dao.getRoleGroupByName(roleName);
	}

	@Override
	public RoleGroup getRoleGroupById(Integer id) {
		// TODO Auto-generated method stub
		return roleGroup_dao.getRoleGroupById(id);
	}


}
